package com.zsrt.oa.product.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zsrt.oa.product.entity.ProductEntity;
import com.zsrt.oa.product.service.ProductService;
import com.zsrt.oa.product.service.impl.ProductServiceImpl;
import com.zsrt.utils.DBUtils;

/**
 * 产品的servlet层
 * @author 罗智壕-13424431416
 *
 * 2017年6月19日
 */
public class ProductController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private ProductService service = new ProductServiceImpl();
       
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) 
    		throws ServletException, IOException {
    	
    	String method = request.getParameter("method");
		if(null != method && !"".equals(method.trim())){
			
			Map<String, Object> map = new HashMap<>();
			ProductEntity entity = new ProductEntity();
			//产品的查询
			if("findAll".equals(method)){
				
				service.findAll(map);
				
			//产品增加
			}else if("add".equals(method)){
				
				//建设公司编号不能为空
				String companyId = request.getParameter("companyId");
				if(companyId != null || !"".equals(companyId)){
					entity.setCompanyId(Integer.parseInt(companyId));
				}
				
				//建设产品价格不能为空
				String productPrice = request.getParameter("productPrice");
				if(productPrice != null || !"".equals(productPrice)){
					entity.setProductPrice(Double.valueOf(productPrice));
				}
				
				//建设产品重量不能为空
				String productWeight = request.getParameter("productWeight");
				if(productWeight != null || !"".equals(productWeight)){
					entity.setProductWeight(Integer.parseInt(productWeight));
				}
				
				entity.setProductName(request.getParameter("productName"));
				
				service.add(entity, map);
				
			//批量删除
			}else if("batchDeleteIds".equals(method)){
				String proIds = request.getParameter("proIds");
				
				service.batchDeleteIds(proIds, map);
				
			//指定查询
			}else if("findById".equals(method)){
				String id = request.getParameter("id");
				
				service.findById(id, map);
				
			//修改
			}else if("updateProductMessage".equals(method)){
				//建设公司编号不能为空
				String companyId = request.getParameter("companyId");
				if(companyId != null || !"".equals(companyId)){
					entity.setCompanyId(Integer.parseInt(companyId));
				}
				
				//建设产品价格不能为空
				String productPrice = request.getParameter("productPrice");
				if(productPrice != null || !"".equals(productPrice)){
					entity.setProductPrice(Double.valueOf(productPrice));
				}
				
				//建设产品重量不能为空
				String productWeight = request.getParameter("productWeight");
				if(productWeight != null || !"".equals(productWeight)){
					entity.setProductWeight(Integer.parseInt(productWeight));
				}
				
				//产品编号不能为空
				String id = request.getParameter("id");
				if(id != null || !"".equals(id)){
					entity.setId(Integer.parseInt(id));
				}
				entity.setProductName(request.getParameter("productName"));
				service.updateProductMessage(entity, map);
			}
			
			DBUtils.toJson(map, response);
		}
    }

}
